/**
 * @swagger
 * /api/room/basic-stats:
 *   get:
 *     tags:
 *       - 房屋管理
 *     summary: 获取房屋基础统计数据
 *     description: 获取房屋数量、面积等基础统计信息
 *     responses:
 *       200:
 *         description: 成功获取基础统计数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                   description: 响应状态码
 *                 message:
 *                   type: string
 *                   example: "基础统计获取成功"
 *                   description: 响应消息
 *                 data:
 *                   type: object
 *                   properties:
 *                     totalRooms:
 *                       type: number
 *                       description: 总房间数
 *                       example: 150
 *                     publicRooms:
 *                       type: number
 *                       description: 公用房间数
 *                       example: 38
 *                     buildingCount:
 *                       type: number
 *                       description: 建筑数量
 *                       example: 12
 *                     totalBuildingArea:
 *                       type: number
 *                       description: 总建筑面积（平方米）
 *                       example: 7920.5
 *                     totalUsageArea:
 *                       type: number
 *                       description: 总使用面积（平方米）
 *                       example: 6780.2
 *                     totalConstructionArea:
 *                       type: number
 *                       description: 总建设面积（平方米）
 *                       example: 8500.0
 *       500:
 *         description: 服务器内部错误
 */
const { query } = require('../../../lib/db')
const { success, error, handleResponse } = require('../../../lib/response')
const { withCors } = require('../../../lib/cors')

/**
 * 处理房屋基础统计请求
 * @param {Object} req - 请求对象
 * @param {Object} res - 响应对象
 */
async function handler(req, res) {

  try {
    const sql = `
      SELECT 
        SUM(ZFJJS) as totalRooms,
        SUM(GYFJS) as publicRooms,  
        SUM(LDS) as buildingCount,
        SUM(XXBXZJZMJ) as totalBuildingArea,
        SUM(XXBXZSYYMJ) as totalUsageArea,
        SUM(ZJZMJ) as totalConstructionArea
      FROM wd_zcgk_bxzyhz
    `

    const result = await query(sql)
    
    if (!result || result.length === 0) {
      return handleResponse(res, error('暂无房产数据', 404))
    }

    const data = result[0]
    
    // 格式化数据，确保数值类型正确
    const formatData = {
      totalRooms: parseInt(data.totalRooms) || 0,
      publicRooms: parseInt(data.publicRooms) || 0,
      buildingCount: parseInt(data.buildingCount) || 0,
      totalBuildingArea: parseFloat(data.totalBuildingArea) || 0,
      totalUsageArea: parseFloat(data.totalUsageArea) || 0,
      totalConstructionArea: parseFloat(data.totalConstructionArea) || 0
    }

    return handleResponse(res, success(formatData, '基础房产统计数据获取成功'))

  } catch (err) {
    console.error('基础查询接口错误:', err)
    return handleResponse(res, error('服务器内部错误', 500))
  }
}

export default withCors(handler, ['GET'])